package com.xinhe.cn.cpbox.controller;


import com.alibaba.fastjson.JSONObject;
import com.xinhe.cn.cpbox.common.Result;
import com.xinhe.cn.cpbox.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HB
 * @date 2021-05-13
 */
@Api(value = "用户相关接口", tags = {"用户相关接口"})
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "用户授权登录，获取用户信息", notes = "用户授权登录，获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "用户同意授权，获取的code", dataTypeClass = String.class),
            @ApiImplicitParam(name = "exuid", value = "推广人的uid(数字)，通过链接携带该参数，无推广人则不传该值", dataTypeClass = String.class)
    })
    @PostMapping("/authorization")
    public Object authorization(@ApiIgnore @RequestBody JSONObject jsonObject) {
        String code = jsonObject.getString("code");
        String exuid = jsonObject.getString("exuid");
        if (StringUtils.isBlank(code)) {
            return Result.paramError().message("code为空");
        }
        return userService.authorization(code, exuid);
    }

    @ApiOperation(value = "授权保存头像和昵称", notes = "授权保存头像和昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "openId", dataTypeClass = String.class),
            @ApiImplicitParam(name = "avatarUrl", value = "微信头像url", dataTypeClass = String.class),
            @ApiImplicitParam(name = "nickName", value = "微信昵称", dataTypeClass = String.class)
    })
    @PostMapping("/savePicAndName")
    public Object savePicAndName(@ApiIgnore @RequestBody JSONObject jsonObject) {
        return userService.savePicAndName(jsonObject);
    }
}

